package com.example.api.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.api.constant.StatusConst;
import com.example.api.dto.AddressDTO;
import com.example.api.exception.BusException;
import com.example.api.mapper.AddressMapper;
import com.example.api.pojo.Address;
import com.example.api.query.AddressQuery;
import com.example.api.service.AddressService;
import com.example.api.utils.PageResult;
import com.example.api.utils.ThreadContextUtil;
import com.example.api.vo.AddressVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    AddressMapper addressMapper;

    @Override
    public void add(AddressDTO addressDTO) {

        //一定要确保从ThreadContextUtil.getData()可以获取到数据

        Long customerId = ThreadContextUtil.getData();
        if(customerId==null){
            throw new BusException("获取会员id失败,添加地址失败","添加地址失败");
        }

        //TODO 如果当前添加的地址是默认的,那么需要将当前会员其他收获地址都设为非默认的

        Address address = new Address();
        address.setCustomerId(ThreadContextUtil.getData());
        BeanUtils.copyProperties(addressDTO,address);

        if(addressDTO.getIsDefault()){
            Address defaultAddr = addressMapper.chadizhi(customerId);
            defaultAddr.setIsDefault(false);
            addressMapper.update(defaultAddr);
        }else{
            address.setIsDefault(false);
        }

        address.setCustomerId(customerId);
        address.setIsDefault(addressDTO.getIsDefault()?true:false);
        addressMapper.add(address);
    }

    @Override
    public PageResult<AddressVO> find(AddressQuery addressQuery) {
        //设置是否需要分页
        PageHelper.startPage(addressQuery.getPage(),addressQuery.getPageSize());  //告诉PageHelper此处需要分页

        Page<AddressVO> page = (Page<AddressVO>) addressMapper.find(addressQuery);

        PageInfo<AddressVO> pageInfo = page.toPageInfo();


        List<AddressVO> addressVOList = pageInfo.getList();
        long total = pageInfo.getTotal();

        PageResult<AddressVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(addressVOList);

        return pageResult;
    }

    @Override
    public AddressVO findById(long id) {
        return addressMapper.findById(id);
    }

    @Override
    public void update(long id, AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        Long customerId = ThreadContextUtil.getData();

        if(addressDTO.getIsDefault()){
            Address defaultAddr = addressMapper.chadizhi(customerId);
            defaultAddr.setIsDefault(false);
            addressMapper.update(defaultAddr);
        }else{
            address.setIsDefault(false);
        }


        address.setIsDefault(addressDTO.getIsDefault()?true:false);
        address.setId(id);
        addressMapper.update(address);
    }

    @Override
    public void deleteById(long id) {
        addressMapper.deleteById(id);
    }

    @Override
    public AddressVO findDefaultAddress(long customerId) {
        AddressQuery addressQuery = new AddressQuery();
        addressQuery.setCustomerId(customerId);
        addressQuery.setIsDefault(true);
        List<AddressVO> addressVOList =  addressMapper.find(addressQuery);
        if(addressVOList == null){
            return null;
        }else{
            return addressVOList.get(0);
        }
    }
}
